<?php 
namespace common\queue\jobs;

use think\queue\Job;
use common\queue\JobAbstract;
use common\service\UserService;
use think\Log;
use common\service\MailService;


class SendMail extends JobAbstract{
	
    public function orderSuccess(Job $job, $data){
    	if(empty($data['user_id']) || empty($data['price']) || empty($data['payWay'])){
    		$job->delete();
    	}
    	try {
    		$userId = $data['user_id'];
    		$price = $data['price'];
    		$payWay = $data['payWay'];
    		$result = UserService::getInstance()->userSendEmail($userId,1, $price, $payWay);
    		if($result){
    			$job->delete();
    		}else if($job->attempts() > 3){
    			$job->delete();
    			$this->failed($data,'success_order');
    		}
    	} catch (\Exception $e) {
    		$job->delete();
    		$data['desc'] = '确认订单发送邮件失败';
    		$data['exception'] = $e->getMessage();
    		$trace = $e->getTrace();
    		$data['file'] = $trace['file'];
    		$data['line'] = $trace['line'];
    		$this->failed($data);
    	}
    }
    
    public function orderCancel(Job $job, $data){
    	
    	if(empty($data['user_id'])){
    		$job->delete();
    	}
    	$userId = $data['user_id'];
    	
    	try {
    		$result = UserService::getInstance()->userSendEmail($userId,2);
    		if($result !== false){
    			$job->delete();
    		}else if($job->attempts() > 3){
    			$job->delete();
    			$this->failed($data,'cancel_order');
    		}
    	} catch (\Exception $e) {
    		$job->delete();
    		$data['desc'] = '取消订单发送邮件失败';
    		$data['exception'] = $e->getMessage();
    		$trace = $e->getTrace();
    		$data['file'] = $trace['file'];
    		$data['line'] = $trace['line'];
    		$this->failed($data);
    	}
    }
    
    public function setpwd(Job $job,$data){
    	if(empty($data['mail']) || empty($data['code']) || empty($data['type']) ){
    		$job->delete();
    	}
    	$mailList[] = $data['mail'];
    	$code = $data['code'];
    	$type = $data['type'];
    	try {
    	 	$res = MailService::getInstance()->setTemplate('sell66_safe_code')->send($mailList, ['%content%'=>[$code]], '验证邮箱有效性');
            $res = json_decode($res, true);
            if($res['result'] === true){
            	$job->delete();
            }elseif($job->attempts() > 3){
            	$job->delete();
    			$this->failed($data,'setpwd');
            }
    	} catch (\Exception $e) {
    		$job->delete();
    		switch ($type){
    			case 'setpwd':
    				$data['desc'] = '设置交易密码发送邮件验证码';
    				break;
    			default:
    				$data['desc'] = '设置交易密码发送邮件验证码';
    				break;
    		}
    		$data['exception'] = $e->getMessage();
    		$trace = $e->getTrace();
    		$data['file'] = $trace['file'];
    		$data['line'] = $trace['line'];
    		$this->failed($data);
    	}
    }
    
    public function failed($data,$source=''){
    	switch ($source){
    		case 'success_order':
    			$path = APP_PATH . '../runtime/logs/mail/success_order/';
    			break;
    		case 'cancel_order':
    			$path = APP_PATH . '../runtime/logs/mail/cancel_order/';
    			break;
    		case 'setpwd':
    			$path = APP_PATH . '../runtime/logs/mail/setpwd/';
    			break;
    		default:
    			$path = APP_PATH . '../runtime/logs/mail/error/';
    			break;
    	}
    	Log::init(['type' => 'File', 'path' => $path]);
    	Log::write('send email failed:'.json_encode($data));
    }

}
